package com.freesky.biz.psbc.service.impl;

import com.alibaba.fastjson.JSON;
import com.freesky.biz.psbc.domain.TPsbcStock;
import com.freesky.biz.psbc.mapper.PsbcStockMapper;
import com.freesky.biz.psbc.service.IPsbcStockService;
import com.freesky.common.core.redis.RedisCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class PsbcStockServiceImpl implements IPsbcStockService {

    @Autowired
    private PsbcStockMapper stockMapper;

    @Autowired
    private RedisCache redisCache;

    private final String REDIS_KEY_PRODUCT = "PSBC:COUPON:";

    @Override
    public TPsbcStock get(String id) {
        return stockMapper.get(id);
    }

    @Override
    public List<TPsbcStock> findList(TPsbcStock entity) {
        return stockMapper.findList(entity);
    }

    @Override
    public int insert(TPsbcStock entity) {
        int rows = stockMapper.insert(entity);
        //TPsbcStock psbcStock = stockMapper.get(entity.getId());
        //setPsbcStockToRedis(REDIS_KEY_PRODUCT+entity.getId(),psbcStock);
        return rows;
    }

    @Override
    public int update(TPsbcStock entity) {
        int rows = stockMapper.update(entity);
        return rows;
    }

    @Override
    public int deleteByIds(String[] ids) {
        int rows = stockMapper.deleteByIds(ids);
        for(String id : ids){
            redisCache.deleteObject(REDIS_KEY_PRODUCT + id);
        }
        return rows;
    }

    public void setPsbcStockToRedis(String key, TPsbcStock psbcStock) {
        log.info("setPsbcStockToRedis:"+key+","+ JSON.toJSONString(psbcStock));
        redisCache.hset(key, "id", psbcStock.getId());
        redisCache.hset(key, "name", psbcStock.getName());
        redisCache.hset(key, "stockType", psbcStock.getStockType());
        redisCache.hset(key, "stockId", psbcStock.getStockId());
        redisCache.hset(key, "amt", psbcStock.getAmt());
        redisCache.hset(key, "totalNum", psbcStock.getTotalNum());
        redisCache.hset(key, "useNum", psbcStock.getUseNum());
        redisCache.hset(key, "leftNum", psbcStock.getLeftNum());
    }
}
